using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core;

namespace SPHelpCollectionBuilder
{
    public partial class RelatedItemChooser : Form
    {
        private bool _hasChanges = false;

        public bool HasChanges
        {
            get { return _hasChanges; }
            set { _hasChanges = value; }
        }

        private HelpItem _currentHelpItem = null;

        public HelpItem CurrentHelpItem
        {
            get { return _currentHelpItem; }
            set { _currentHelpItem = value; }
        }

        private List<HelpItemRelatedItemEntry> _relatedItems = null;

        public List<HelpItemRelatedItemEntry> RelatedItems
        {
            get { return _relatedItems; }
            set { _relatedItems = value; }
        }

        public RelatedItemChooser()
        {
            InitializeComponent();
        }

        private void buttonSelectHelpItem_Click(object sender, EventArgs e)
        {
            using (TreeViewPicker picker = new TreeViewPicker())
            {
                DialogResult dResult = picker.ShowDialog(this);
                if (dResult == DialogResult.OK)
                {
                    _currentHelpItem = (HelpItem)picker.SelectedTreeNodeTag.MirrorObject;
                    lblSelectedHelpItem.Text = _currentHelpItem.id;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (_currentHelpItem == null)
            {
                _currentHelpItem = null;
                MessageBox.Show("Please choose a parent help item");
                return;
            }

            if (string.IsNullOrEmpty(textBoxSortOrder.Text))
            {
                _currentHelpItem = null;
                MessageBox.Show("Please choose sort order");
                return;
            }

            int sortNumer = int.Parse(textBoxSortOrder.Text);

            HelpItemRelatedItemEntry entry = new HelpItemRelatedItemEntry();
            entry.sortOrder = sortNumer;
            entry.Value = _currentHelpItem.id;

            listBoxRelatedItems.Items.Add(entry);

            ClearControls();
            this.HasChanges = true;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (listBoxRelatedItems.SelectedItem != null)
                listBoxRelatedItems.Items.RemoveAt(listBoxRelatedItems.SelectedIndex);

            this.HasChanges = true;
        }

        private void ClearControls()
        {
            _currentHelpItem = null;
            textBoxSortOrder.Text = string.Empty;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (RelatedItems == null && listBoxRelatedItems.Items.Count > 0)
                RelatedItems = new List<HelpItemRelatedItemEntry>();

            foreach (object item in listBoxRelatedItems.Items)
            {
                RelatedItems.Add((HelpItemRelatedItemEntry)item);
            }

            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void textBoxSortOrder_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }   
        }

        private void listBoxRelatedItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = true;
        }

        public void LoadData(HelpItemManifestEntry entry)
        {
            if (entry.relatedItemsPoitingToMe != null)
            {
                listBoxRelatedItems.Items.Clear();
                foreach (HelpItemRelatedItemEntry p in entry.relatedItemsPoitingToMe)
                {
                    listBoxRelatedItems.Items.Add(p);
                }
            }
        }
    }
}